package Vista;

import Controlador.CPrincipal;
import VO.VOMensaje;
import java.rmi.RemoteException;
import java.util.Arrays;
import javax.swing.JOptionPane;
import org.jdesktop.application.Action;

public class VPrincipal extends javax.swing.JFrame {
    CPrincipal control;
    
    public VPrincipal(CPrincipal c) {
        control = c;
        initComponents();
    }

    @Action
    public void DesencriptarPresionado() throws RemoteException {
        if(this.revisaForm()) {
            char [] p;
            String l="";
            VOMensaje m = new VOMensaje();
            m.setMsj(this.mensaje_rec.getText());
            p = this.Clave_desen.getPassword();
            for(int i=0; i<p.length; i++)
                l+=p[i];
            this.control.Desencriptar(m, l);
        }
    }

    @Action
    public void EnviarPresionado() throws RemoteException {
        if(this.revisaForm()) {
            char [] p = new char[8];
            String l="";
            VOMensaje m = new VOMensaje();
            m.setMsj(mensaje_env.getText());
            m.setReceptor(mensaje_env.getText());
            Arrays.fill(p, '0');
            p = this.Clave_enc.getPassword();
            for(int i=0; i<p.length; i++)
                l+=p[i];
            if(this.control.Enviar(m, l)==0) {
                this.Clave_enc.setText("");
                this.mensaje_env.setText("");
            }
        }
    }

    public boolean revisaForm() {
        boolean e=true;
        if(this.jTabbedPane1.getSelectedIndex()==0) {
            if(this.mensaje_env.getText()==null || this.mensaje_env.getText().compareTo("")==0) {
                e=false;
                this.err_msj1.setText("Debes ingresar un mensaje");
            }
            if(this.Clave_enc.getPassword().length!=8) {
                e=false;
                this.err_key1.setText("La clave debe ser de 8 caracteres");
            }
        } else {
            if(this.mensaje_rec.getText()==null || this.mensaje_rec.getText().compareTo("")==0) {
                e=false;
                this.err_msj2.setText("Debes ingresar un mensaje");
            }
            if(this.Clave_desen.getPassword().length!=8) {
                e=false;
                this.err_key1.setText("La clave debe ser de 8 caracteres");
            }
        }
        return e;
    }
    
    public int Cerrar() throws RemoteException {
        return this.control.logout();
    }

    public void VentanaFeedback(String m, String t) {
        JOptionPane.showMessageDialog(this, m, t, JOptionPane.INFORMATION_MESSAGE);
    }
    public void setIden(String string) {
        this.iden1.setText(string);
        this.iden2.setText(string);
    }
    public void Recibir(VOMensaje m) {
        this.jTabbedPane1.setSelectedIndex(1);
        this.mensaje_rec.setText(m.getMsj());
    }
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        mensaje_env = new javax.swing.JTextPane();
        jLabel1 = new javax.swing.JLabel();
        Enviar = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        Detalle_enc = new javax.swing.JButton();
        Clave_enc = new javax.swing.JPasswordField();
        err_key1 = new javax.swing.JLabel();
        err_msj1 = new javax.swing.JLabel();
        iden1 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        mensaje_rec = new javax.swing.JTextPane();
        jLabel3 = new javax.swing.JLabel();
        Desencriptar = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        Detalle_desen = new javax.swing.JButton();
        Clave_desen = new javax.swing.JPasswordField();
        err_key2 = new javax.swing.JLabel();
        iden2 = new javax.swing.JLabel();
        err_msj2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setName("Form"); // NOI18N
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jTabbedPane1.setName("jTabbedPane1"); // NOI18N

        jPanel1.setName("jPanel1"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        mensaje_env.setName("mensaje_env"); // NOI18N
        jScrollPane1.setViewportView(mensaje_env);

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(cliente.ClienteApp.class).getContext().getResourceMap(VPrincipal.class);
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(cliente.ClienteApp.class).getContext().getActionMap(VPrincipal.class, this);
        Enviar.setAction(actionMap.get("EnviarPresionado")); // NOI18N
        Enviar.setText(resourceMap.getString("Enviar.text")); // NOI18N
        Enviar.setName("Enviar"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        Detalle_enc.setAction(actionMap.get("quit")); // NOI18N
        Detalle_enc.setText(resourceMap.getString("Detalle_enc.text")); // NOI18N
        Detalle_enc.setName("Detalle_enc"); // NOI18N

        Clave_enc.setText(resourceMap.getString("Clave_enc.text")); // NOI18N
        Clave_enc.setName("Clave_enc"); // NOI18N

        err_key1.setForeground(resourceMap.getColor("err_key1.foreground")); // NOI18N
        err_key1.setText(resourceMap.getString("err_key1.text")); // NOI18N
        err_key1.setName("err_key1"); // NOI18N

        err_msj1.setForeground(resourceMap.getColor("err_msj1.foreground")); // NOI18N
        err_msj1.setText(resourceMap.getString("err_msj1.text")); // NOI18N
        err_msj1.setName("err_msj1"); // NOI18N

        iden1.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        iden1.setText(resourceMap.getString("iden1.text")); // NOI18N
        iden1.setName("iden1"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 409, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(err_msj1, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 76, Short.MAX_VALUE)
                        .addComponent(iden1, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                .addComponent(err_key1, javax.swing.GroupLayout.DEFAULT_SIZE, 158, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(Detalle_enc)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(Enviar))
                            .addComponent(Clave_enc, javax.swing.GroupLayout.DEFAULT_SIZE, 372, Short.MAX_VALUE))))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(err_msj1, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(iden1, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 102, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(Clave_enc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(err_key1, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(Detalle_enc)
                    .addComponent(Enviar))
                .addContainerGap())
        );

        jTabbedPane1.addTab(resourceMap.getString("jPanel1.TabConstraints.tabTitle"), jPanel1); // NOI18N

        jPanel2.setName("jPanel2"); // NOI18N

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        mensaje_rec.setEditable(false);
        mensaje_rec.setName("mensaje_rec"); // NOI18N
        jScrollPane2.setViewportView(mensaje_rec);

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        Desencriptar.setAction(actionMap.get("DesencriptarPresionado")); // NOI18N
        Desencriptar.setText(resourceMap.getString("Desencriptar.text")); // NOI18N
        Desencriptar.setName("Desencriptar"); // NOI18N

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        Detalle_desen.setText(resourceMap.getString("Detalle_desen.text")); // NOI18N
        Detalle_desen.setName("Detalle_desen"); // NOI18N

        Clave_desen.setText(resourceMap.getString("Clave_desen.text")); // NOI18N
        Clave_desen.setName("Clave_desen"); // NOI18N

        err_key2.setForeground(resourceMap.getColor("err_key2.foreground")); // NOI18N
        err_key2.setText(resourceMap.getString("err_key2.text")); // NOI18N
        err_key2.setName("err_key2"); // NOI18N

        iden2.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        iden2.setText(resourceMap.getString("iden2.text")); // NOI18N
        iden2.setName("iden2"); // NOI18N

        err_msj2.setForeground(resourceMap.getColor("err_msj2.foreground")); // NOI18N
        err_msj2.setName("err_msj2"); // NOI18N

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 409, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 228, Short.MAX_VALUE)
                        .addComponent(iden2, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                                .addComponent(err_key2, javax.swing.GroupLayout.DEFAULT_SIZE, 198, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(Detalle_desen)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(Desencriptar))
                            .addComponent(Clave_desen, javax.swing.GroupLayout.DEFAULT_SIZE, 372, Short.MAX_VALUE))))
                .addContainerGap())
            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup()
                    .addGap(139, 139, 139)
                    .addComponent(err_msj2, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(140, Short.MAX_VALUE)))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(iden2, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(7, 7, 7)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 101, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(Clave_desen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(Desencriptar)
                        .addComponent(Detalle_desen))
                    .addComponent(err_key2, javax.swing.GroupLayout.DEFAULT_SIZE, 23, Short.MAX_VALUE))
                .addContainerGap())
            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup()
                    .addGap(111, 111, 111)
                    .addComponent(err_msj2, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(74, Short.MAX_VALUE)))
        );

        jTabbedPane1.addTab(resourceMap.getString("jPanel2.TabConstraints.tabTitle"), jPanel2); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 434, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 227, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(30, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
    }//GEN-LAST:event_formWindowClosed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        try {
            this.Cerrar();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }//GEN-LAST:event_formWindowClosing

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPasswordField Clave_desen;
    private javax.swing.JPasswordField Clave_enc;
    private javax.swing.JButton Desencriptar;
    private javax.swing.JButton Detalle_desen;
    private javax.swing.JButton Detalle_enc;
    private javax.swing.JButton Enviar;
    private javax.swing.JLabel err_key1;
    private javax.swing.JLabel err_key2;
    private javax.swing.JLabel err_msj1;
    private javax.swing.JLabel err_msj2;
    private javax.swing.JLabel iden1;
    private javax.swing.JLabel iden2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTextPane mensaje_env;
    private javax.swing.JTextPane mensaje_rec;
    // End of variables declaration//GEN-END:variables
    
}